package com.llk.myIoLearn.reactorBase.myreactor.httpv2;

import java.nio.channels.*;
import java.net.*;

public class test {
    public static void main(String[] args) throws Exception {
        Selector selector = Selector.open();
        ServerSocketChannel serverChannel = ServerSocketChannel.open();
        serverChannel.bind(new InetSocketAddress(0));
        serverChannel.configureBlocking(false);
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);

        // 启动注册线程（尝试在 select() 阻塞期间注册新通道）
        new Thread(() -> {
            try {
                Thread.sleep(2000);
                SocketChannel clientChannel = SocketChannel.open();
                clientChannel.configureBlocking(false);
                System.out.println("注册线程尝试注册新通道...");
                clientChannel.register(selector, SelectionKey.OP_READ); // 此处可能阻塞
                System.out.println("注册成功！");
            } catch (Exception e) { e.printStackTrace(); }
        }).start();

        // 主线程阻塞在 select()，持续 5 秒
        System.out.println("主线程进入 select() 阻塞");
        selector.select();
        System.out.println("select() 返回");
    }
}